const LeaveMessage = require("../../model/leave_message.model");

const User = require("../../model/user.model");

// 生成树形结构
function buildTree(arr, parentId = null) {
  const tree = [];
  for (const item of arr) {
    if (item.parent_id === parentId) {
      const children = buildTree(arr, item.id);
      let parentData = { parentTitle: null };
      if (children.length > 0) {
        item.children = children;
        item.children.forEach((parentData) => {
          parentData.parentTitle = item.title;
        });
      }

      tree.push(item);
    }
  }
  return tree;
}
class LeaveMessageService {
  // async getLeaveMessage(args) {
  //   const { current = 1, pageSize = 10, ...remainingParameters } = args;
  //   const offset = (current - 1) * pageSize;
  //   const { rows: list, count: total } = await LeaveMessage.findAndCountAll({
  //     limit: Number(pageSize),
  //     offset: offset,
  //     where: {
  //       ...remainingParameters,
  //     },
  //     order: [["createdAt",'DESC']],
  //     include: [
  //       {
  //         model: User,
  //         as: "userInfo",
  //         attributes: ["id", "avatar_url","nickname"],
  //       },
  //     ],
  //   });
  //   return {
  //     current,
  //     pageSize,
  //     total,
  //     list,
  //   };
  // }
  async getLeaveMessage(args) {
    const { current = 1, pageSize = 10, ...remainingParameters } = args;
    const offset = (current - 1) * pageSize;
    const { rows, count: total } = await LeaveMessage.findAndCountAll({
      limit: Number(pageSize),
      offset: offset,
      where: {
        ...remainingParameters,
      },
      order: [["createdAt", "DESC"]],
      include: [
        {
          model: User,
          as: "userInfo",
          attributes: ["id", "avatar_url", "nickname"],
        },
      ],
    });
    const list = buildTree(JSON.parse(JSON.stringify(rows)));
    return {
      current,
      pageSize,
      total,
      list,
    };
  }
  async addLeaveMessage(args) {
    const { id, createdAt, is_it_enabled, updatedAt, ...remainingParameters } =
      args;
    const res = await LeaveMessage.create(remainingParameters);
    return res;
  }
  async updateLeaveMessage(args) {
    const { id, createdAt, updatedAt, ...remainingParameters } = args;
    const response = await LeaveMessage.update(
      {
        ...remainingParameters,
      },
      {
        where: {
          id,
        },
      }
    );
    return response;
  }
  async updateLeaveMessage(args) {
    const { id, createdAt, updatedAt, ...remainingParameters } = args;
    const response = await LeaveMessage.update(
      {
        ...remainingParameters,
      },
      {
        where: {
          id,
        },
      }
    );
    return response;
  }
  async likeLeaveMessage(args) {
    const { id, createdAt, updatedAt, ...remainingParameters } = args;
    const response = await LeaveMessage.findOne({
      where: {
        id,
      },
    });
    const like = JSON.parse(JSON.stringify(response)).like;
    await LeaveMessage.update(
      {
        like: like + 1,
      },
      {
        where: {
          id,
        },
      }
    );
    return null;
  }
  async deleteLeaveMessage(args) {
    const { id } = args;
    const res = await LeaveMessage.destroy({
      where: {
        id,
      },
    });
    return res;
  }
}

module.exports = new LeaveMessageService();
